/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.counttimer;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.*;
import android.app.AlertDialog ;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.ColorPickerDialog ;

import com.android.MyDatabase ;
import com.android.NumberPicker;

/**
 *
 * @author ksemuldie
 */
public class RepeatActivity extends Activity {
  private int id_session = 0 ;
  private int id_repeat = 0 ;
  static final int COLOR_DIALOG_ID = 0;
  public static final String VAL_ID_REPEAT = "id_repeat" ;

  private int color = 0 ;
  private String repeatLabel = "" ;

  @Override
  protected void onResume(){
    super.onResume();

    setContentView(R.layout.edit_repeat);

    Bundle extras = getIntent().getExtras();
    id_session = extras.getInt(MainActivity.VAL_ID_SESSION) ;

    String action = extras.getString("action") ;

    if(action.compareTo("edit") == 0) {
      id_repeat = extras.getInt(RepeatActivity.VAL_ID_REPEAT) ;
      
      /**
       * Extract repeat and compile for edit repeat
       */
      MyDatabase db=new MyDatabase(getApplicationContext());
      db.open();  //apriamo il db

      Cursor CRepeat = db.fetchRepeat(id_session , id_repeat) ;
      
      startManagingCursor(CRepeat);
      /**
       * Get Cursor value and compile data in field
       */

      //First row exist
      if(CRepeat.moveToFirst()) {
        //Toast.makeText(getBaseContext(),"Estratta una riga",Toast.LENGTH_SHORT).show();
        AutoCompleteTextView newRepeatLabelView = (AutoCompleteTextView) findViewById(R.id.r_label);

        newRepeatLabelView.setText(CRepeat.getString(2));


        TextView TVColor = (TextView)findViewById(R.id.tv_color) ;
        color = CRepeat.getInt(4) ;
        TVColor.setBackgroundColor(color);


        NumberPicker NPRepeater = (NumberPicker)findViewById(R.id.np_repeat) ;
        NPRepeater.setCurrent(CRepeat.getInt(3));
      }else {
        //Toast.makeText(getBaseContext(),"ERROR RIGA "+id_session+" "+id_repeat+" nON TROVATA",Toast.LENGTH_SHORT).show();
      }
      db.close() ;

      Button SaveBtn = (Button)findViewById(R.id.submit);
      SaveBtn.setText(getString(R.string.save)) ;
      SaveBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
          MyDatabase db=new MyDatabase(getApplicationContext());
          db.open();  //apriamo il db

          AutoCompleteTextView newRepeatLabelView = (AutoCompleteTextView) findViewById(R.id.r_label);
          
          NumberPicker NPRepeater = (NumberPicker)findViewById(R.id.np_repeat) ;
          NPRepeater.setOnChangeListener(mNumberListener) ;

          TextView TVColor = (TextView)findViewById(R.id.tv_color) ;

          String newRepeatText = newRepeatLabelView.getText().toString() ;

          db.editRepeat(id_repeat , newRepeatText , NPRepeater.getCurrent() , color) ;

          db.close() ;

/*
          Intent myIntent = new Intent(view.getContext(), EditSessionActivity.class);
          myIntent.putExtra(MainActivity.VAL_ID_SESSION, id_session) ;
          startActivity(myIntent) ;
*/
          Toast.makeText(getBaseContext(),
            getString(R.string.updated_repeat),
            Toast.LENGTH_SHORT).show();

          RepeatActivity.this.finish();
        }
      });

    }else {
      id_repeat = 0 ;

      Button SaveBtn = (Button)findViewById(R.id.submit);
      SaveBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
          MyDatabase db=new MyDatabase(getApplicationContext());
          db.open();  //apriamo il db

          AutoCompleteTextView newRepeatLabelView = (AutoCompleteTextView) findViewById(R.id.r_label);
          NumberPicker NPRepeater = (NumberPicker)findViewById(R.id.np_repeat) ;
          TextView TVColor = (TextView)findViewById(R.id.tv_color) ;

          String newRepeatText = newRepeatLabelView.getText().toString() ;

          db.insertRepeat(id_session , newRepeatText , NPRepeater.getCurrent() , color) ;

          db.close() ;

          /*Intent myIntent = new Intent(view.getContext(), EditSessionActivity.class);
          myIntent.putExtra(MainActivity.VAL_ID_SESSION, id_session) ;
          startActivity(myIntent) ;*/

          Toast.makeText(getBaseContext(),
            getString(R.string.added_repeat),
            Toast.LENGTH_SHORT).show();

          RepeatActivity.this.finish();
        }
      });
    }

    Button ColorPickerBtn = (Button)findViewById(R.id.color_picker);
    ColorPickerBtn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        showDialog(COLOR_DIALOG_ID);
      }
    });

    
  }

  @Override
  //protected void onStart(Bundle savedInstanceState) {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.edit_session);

  }

  private NumberPicker.OnChangedListener mNumberListener =
    new   NumberPicker.OnChangedListener() {

    public void onChanged(NumberPicker picker, int oldVal, int newVal) {
      /**
       * @TODO Verificare se è da togliere
       */
      //Toast.makeText(getBaseContext(),"Numero selezionato "+newVal,Toast.LENGTH_SHORT).show();
    }
  };

  private ColorPickerDialog.OnColorChangedListener mColorSetListener =
      new ColorPickerDialog.OnColorChangedListener() {
        public void colorChanged(int newColor) {
          
          //Button ColorPickerBtn = (Button)findViewById(R.id.color_picker);
          TextView ColorPickerTV = (TextView)findViewById(R.id.tv_color);
          ColorPickerTV.setBackgroundColor(newColor);
          color = newColor ;
        }
    };

  @Override
  protected Dialog onCreateDialog(int id) {
      switch (id) {
      case COLOR_DIALOG_ID:
          return new ColorPickerDialog(this,
                  mColorSetListener, color);
      }
      return null;
  }
}
